package com.five.taotao.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.five.taotao.Application.MyApplication;
import com.five.taotao.R;
import com.five.taotao.http.CallBack;
import com.five.taotao.setup.HttpActivity;
import com.five.taotao.util.Constants;
import com.five.taotao.util.VolleyUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * gaomengjiao
 * Created by MQ on 2016/11/3.
 */
public class RegisterActivity extends AppCompatActivity {

    @BindView(R.id.back)
    ImageView back;

    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.usernameWrapper)
    TextInputLayout usernameWrapper;

    @BindView(R.id.btn)
    TextView btn;
    @BindView(R.id.xieyi)
    TextView xieyi;
    @BindView(R.id.pwd)
    EditText pwd;
    @BindView(R.id.pwdWrapper)
    TextInputLayout pwdWrapper;

    private String phones;
    private String passwords;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_id);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back, R.id.login, R.id.btn, R.id.xieyi})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.login:
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.btn://把注册的号码写到数据库
                hideKeyboard();
                phones = usernameWrapper.getEditText().getText().toString();
                passwords = pwdWrapper.getEditText().getText().toString();
                if ( !checkPhonePattern(phones)) {
                    usernameWrapper.setError("请输入正确的手机号!");
                } else if (!validatePassword(passwords)) {
                    pwdWrapper.setError("请输入五位以上密码!");
                } else {
                    usernameWrapper.setErrorEnabled(false);
                    pwdWrapper.setErrorEnabled(false);

                    OkHttpUtils
                            .post()
                            .url(Constants.AU_URL)
                            .addParams("phone", phones)
                            .addParams("pwd", passwords)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    MyApplication.toast.toastShow("注册失败");
                                }

                                @Override
                                public void onResponse(String response, int id) {

                                    if (response.equals("-1")) {
                                        MyApplication.toast.toastShow("用户已存在！");
                                    }else if (response.equals("0")) {
                                        MyApplication.toast.toastShow("注册失败！");
                                    }else if (response.equals("1")){
                                        MyApplication.toast.toastShow("注册成功");
                                        finish();
                                    }

                                }
                            });

                }
                break;
            case R.id.xieyi:
                Intent intent1 = new Intent(this, HttpActivity.class);
                startActivity(intent1);
                break;
        }
    }

    /**
     * 自动隐藏虚拟键盘
     */
    private void hideKeyboard() {
        View view = getCurrentFocus();
        if (view != null) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 检测密码是否正确，   后期还会再改
     *
     * @param passwords 密码
     * @return 密码是否大于5位
     */
    public boolean validatePassword(String passwords) {
        return passwords.length() > 5;
    }

    //验证手机号的正则表达式
    private static final String PHONE_PATTERN = "[1][34578]\\d{9}";
    private Pattern pattern = Pattern.compile(PHONE_PATTERN);
    private Matcher matcher;

    /**
     * 检查输入的号码是否是手机号
     *
     * @param phones 输入的手机号
     * @return
     */
    public boolean checkPhonePattern(String phones) {
        matcher = pattern.matcher(phones);
        return matcher.matches();
    }



}
